package com.gem.sms.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**权限过滤器,用来判断用户是否登录*/
@WebFilter(filterName = "PermissionFilter", urlPatterns = "/permission/*")
public class PermissionFilter implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		//把ServletRequest和ServletResponse转换成HTTP版本
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		//判断用户是否登录,如果登录了,则显示列表数据,如果没登录,重回登录页面
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("stu");
		if (obj == null) {
			//说明没有登录!
			response.sendRedirect(request.getContextPath() + "/stu/index?info=2");
		} else {
			//说明已经登录,则放行
			chain.doFilter(request, response);
		}
	}

	@Override
	public void destroy() {
	}
}
